﻿//Copyright (C) <2013>  <jonathan cleeve norton> All Rights Reserved 
//Contact jon.norton@fin-plus.co.uk website <http://www.fin-plus.co.uk/>
using System;
using System.ComponentModel;
using FinPlusCommon;
using log = Logger.Logger;

namespace FinPlusComponents
{
    public class QLParseBondFutTrade : FinPlusComponent
    {
        private enum Params { ParseTradeAction, ResponseAction, BondFuture, Instrument, SettlementDate, QuoteRef, Quote, Size, Book, Counterparty, MarketName, CacheName, TradeId, BondName, Nominal, Settlement, Price }
        private readonly IFinPlusComp _instrumentBuildConn, _tradeAdaptorConn;
        private readonly FinPlusConnectDB _tradeSaveConn;
        private readonly IFunction _tradeAdaptor;

        //construct
        public QLParseBondFutTrade(IFinPlusComp instrumentBuilds, IFinPlusComp tradeAdaptors, IFinPlusComp tradeSave)
        { 
            _instrumentBuildConn = instrumentBuilds;
            _tradeAdaptorConn = tradeAdaptors;
            _tradeSaveConn = (FinPlusConnectDB)tradeSave;
            _tradeAdaptor = _tradeAdaptorConn.Adaptor.First("Name='InstrumentType' And Value='BDFuture'");
            _tradeAdaptor.Get(Params.ParseTradeAction.ToString(), Parse_Trade);
            IsValid = true;
        }

        //common control interface
        public override void Dispose()
        {
            try
            {
                if (_tradeSaveConn != null)
                    _tradeSaveConn.Dispose();
            }
            catch (Exception e)
            {
                IsValid = log.Log(Logger.Level.Warning, Id, Config, e.Message, e);
            }
        }

        public override void CmdExecute(IFunction command)
        {
            try
            {
                CmdCapture(command);

                switch ((Cmds)Enum.Parse(typeof(Cmds), command.Name, true))
                {
                    case Cmds.QuoteAccept: ExecutionParse(command); break;
                    case Cmds.QuoteAcceptPublish: ExecutionParse(command); break;
                    default: throw new FormatException (string.Format("event not recognised {0}", command.Name));
                }
            }
            catch (CmdExecuteException e)
            {
               IsValid = log.Log(Logger.Level.Error, Id, Config, e.Message, e);
            }
        }

        //private
        private bool ExecutionParse(IFunction func)
        {
            try
            {
                //TODO Parse

                //save trade
                //_tradeSaveConn.Save(func);
                
                //respond with trade for testing
                //trade.Add(Params.Instrument.ToString(), instrument);
                //_tradeAdaptor.GetItem(Params.ResponseAction.ToString()).SetValue(trade);
                return true;
            }
            catch (Exception e)
            {
                return IsValid = log.Log(Logger.Level.Error, Id, Config, func.ToString(), e); 
            }
        }

        //cmds
        private enum Cmds { QuoteAccept, QuoteAcceptPublish };

        //events 
        private void Parse_Trade(object s, PropertyChangedEventArgs a) { CmdRouter(s); }//QuoteAccept && QuotePublishAccept
    }
}
